package org.dromara.apartment.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.area.core.Area;
import org.dromara.common.area.core.utils.AreaUtils;
import org.dromara.common.core.domain.vo.CityVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.apartment.domain.vo.ApartmentInfoVo;
import org.dromara.apartment.domain.bo.ApartmentInfoBo;
import org.dromara.apartment.service.IApartmentInfoService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 公寓管理
 * 前端访问路由地址为:/apartment/apartment
 *
 * @author ckj
 * @date 2025-06-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/apartment")
public class ApartmentInfoController extends BaseController {

    private final IApartmentInfoService apartmentInfoService;

    /**
     * 查询公寓管理列表
     */
    @SaCheckPermission("apartment:apartment:list")
    @GetMapping("/list")
    public TableDataInfo<ApartmentInfoVo> list(ApartmentInfoBo bo, PageQuery pageQuery) {
        return apartmentInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出公寓管理列表
     */
    @SaCheckPermission("apartment:apartment:export")
    @Log(title = "公寓管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ApartmentInfoBo bo, HttpServletResponse response) {
        List<ApartmentInfoVo> list = apartmentInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "公寓管理", ApartmentInfoVo.class, response);
    }

    /**
     * 获取公寓管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("apartment:apartment:query")
    @GetMapping("/{id}")
    public R<ApartmentInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable("id") Long id) {
        return R.ok(apartmentInfoService.queryById(id));
    }

    /**
     * 新增公寓管理
     */
    @SaCheckPermission("apartment:apartment:add")
    @Log(title = "公寓管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ApartmentInfoBo bo) {
        return toAjax(apartmentInfoService.insertByBo(bo));
    }

    /**
     * 修改公寓管理
     */
    @SaCheckPermission("apartment:apartment:edit")
    @Log(title = "公寓管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ApartmentInfoBo bo) {
        return toAjax(apartmentInfoService.updateByBo(bo));
    }

    /**
     * 删除公寓管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("apartment:apartment:remove")
    @Log(title = "公寓管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("ids") Long[] ids) {
        return toAjax(apartmentInfoService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 恢复公寓信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("apartment:apartment:recover")
    @Log(title = "恢复公寓信息", businessType = BusinessType.DELETE)
    @PutMapping("recover/{ids}")
    public R<Void> recover(@NotEmpty(message = "主键不能为空")
                           @PathVariable("ids") Long[] ids) {
        return toAjax(apartmentInfoService.recoverWithValidByIds(List.of(ids), true));
    }


    /**
     * 永久删除公寓信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("apartment:apartment:foreverDelete")
    @Log(title = "永久删除公寓信息", businessType = BusinessType.DELETE)
    @DeleteMapping("foreverDelete/{ids}")
    public R<Void> foreverDelete(@NotEmpty(message = "主键不能为空")
                                 @PathVariable("ids") Long[] ids) {
        return toAjax(apartmentInfoService.foreverDeleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 公寓发布状态修改
     */
    @SaCheckPermission("apartment:apartment:edit")
    @Log(title = "公寓发布状态修改", businessType = BusinessType.UPDATE)
    @PutMapping("/changeReleaseStatus")
    public R<Void> changeReleaseStatus(@RequestBody ApartmentInfoBo bo) {
        return toAjax(apartmentInfoService.updateStatus(bo));
    }


}
